package com.biu.billiards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biu.billiards.model.bo.CouponBO;
import com.biu.billiards.model.entity.Coupon;
import com.biu.billiards.mapper.CouponMapper;
import com.biu.billiards.model.vo.coupon.AddCouponVo;
import com.biu.billiards.model.vo.coupon.GetCouponVo;
import com.biu.billiards.model.vo.coupon.UpdateCouponVO;
import com.biu.billiards.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-09-25
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {


    @Resource
    private CouponMapper couponMapper;
    @Override
    public IPage<CouponBO> getCouponPage(GetCouponVo getCouponVo) {
        QueryWrapper<Coupon> query = new QueryWrapper<>();
        //新建分页条件
        Page<Coupon> p = new Page<>(getCouponVo.getCurrentPage(),getCouponVo.getPageSize());

        IPage<Coupon> userIPage = couponMapper.selectPage(p,query);
        IPage<CouponBO> page = new Page<>();
        List<CouponBO> adminDetailBOList = new ArrayList<>();
        for (Coupon admin: userIPage.getRecords()){
            CouponBO adminDetailBO = new CouponBO();
            BeanUtils.copyProperties(admin,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());
        return page;
    }

    @Override
    public Boolean updateCoupon(UpdateCouponVO updateCouponVO) {
        QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("billiards_id",updateCouponVO.getCouponId());

        Coupon coupon = couponMapper.selectOne(queryWrapper);

        return couponMapper.update(coupon, queryWrapper) == 1;
    }

    @Override
    public Boolean addCoupon(AddCouponVo addCouponVo) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(addCouponVo,coupon);
        return couponMapper.insert(coupon) == 1;
    }

    @Override
    public Boolean deleteCoupon(Integer couponId) {
        QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("billiards_id",couponId);
        return couponMapper.delete(queryWrapper) == 1;
    }
}
